var fieldCache={};
//保存账号
function saveAccount(){
    var act=$('#act');
    var isValid=$('#accountFrm').form('validate');
    if(!isValid){
        return;
    }

    var formData={};
    var selectedType=$('#typeId').combobox('getValue');
    formData.typeId=selectedType==""?0:parseInt(selectedType);
    var selectedProjs=$('#projId').combobox('getValues');
    selectedProjs.remove(0);//删除'无'项
    if(selectedProjs.length>0){
        formData.projIds=toIntArray(selectedProjs);
    }
    formData.accountName=$('#accountName').textbox('getValue');
    formData.fieldUser=$('#fieldUser').textbox('getValue');
    formData.fieldPwd=$.trim($('#fieldPwd').textbox('getValue'));
    var hostId=$('#hostId').val();
    if(hostId!=""){
        formData.hostId=parseInt(hostId);
      //console.log("host:"+formData.hostId)
    }
    if(act="edit"){
        formData.accountId=parseInt($('#accountId').val());
        var oldPwd=$.trim($('#oldFieldPwd').val());
        if(oldPwd==formData.fieldPwd){
            formData.fieldRePwd=formData.fieldPwd;//没改过密码
        }else{
            formData.fieldRePwd=$.trim($('#fieldRePwd').textbox('getValue'));
        }
    }else{
        formData.fieldRePwd=$.trim($('#fieldRePwd').textbox('getValue'));
    }
    //console.log("pwd:"+formData.fieldPwd+"|repwd:"+formData.fieldRePwd);
    if(formData.fieldPwd!=""&&formData.fieldPwd!=formData.fieldRePwd){
        showError("两次密码不相同");
        return;
    }

    formData.fieldUrl=$('#fieldUrl').textbox('getValue');
    formData.fieldRemark=$('#fieldRemark').textbox('getValue');

    if(selectedType>0){
        var fields=fieldCache[formData.typeId];
        var fieldOther={}
        $.each(fields,function(index,elem){
            if (elem.valueType==1) {
                fieldOther[elem.fieldKey]=isCheck(elem.fieldKey);
            }else if(elem.valueType==2){
                fieldOther[elem.fieldKey]=$('#'+elem.fieldKey).numberbox('getValue');
            }else if(elem.valueType==3){
                fieldOther[elem.fieldKey]=$('#'+elem.fieldKey).combobox('getValue');
            }else{
                fieldOther[elem.fieldKey]=$('#'+elem.fieldKey).textbox('getValue');
            }
        });
        formData.fieldOther=JSON.stringify(fieldOther);
    }
    $.ajax({
        type: "POST",
        url: "/account/saveaccount",
        contentType: "application/json; charset=utf-8",
        data:  JSON.stringify(formData),
        dataType: "json",
        success: function (result) {
            handleJsonResult(result,function(data){
                parent.closeAccountDlg(true);
            })
        }
    });
}
const opsCache={}
//创建动态属性表单组件
function createOtherField(fields,selectorId){
    var wrapper=$('#'+selectorId);
    wrapper.empty();
    if (fields.length>0){
        $.each(fields,function(index,elem){
            var dataoptions="label:\""+elem.fieldName+"\",labelWidth:\"110\"";
            
            var eleHtml='<div class="inputctl"> <input name="'+elem.fieldKey+'" id="'+elem.fieldKey+'" class="';
            if(elem.isRequired){
                dataoptions=dataoptions+", required:true"
                eleHtml+="required "
            } 
            if(elem.valueType==1){
                eleHtml+='easyui-checkbox" value="true"';
               // console.log(elem.fieldValue);
                if(elem.fieldValue==true){
                   // console.log("---");
                    dataoptions+=',checked:true';
                }
            }else if(elem.valueType==2){
                eleHtml+='easyui-numberbox"';
                if (elem.maxLen!=0) {
                    dataoptions=dataoptions+",max:"+elem.maxLen
                }
                dataoptions+=",width:'430px'";
            }else if(elem.valueType==3){
               // opsCache[elem.fieldKey]=elem.valueRule;
                eleHtml+='easyui-combobox"';
                dataoptions+=',panelHeight:"150px",width:"430px",valueField:"value",textField:"text",data:'+elem.valueRule
            }else{
                eleHtml+=(elem.isCiphertext?"easyui-passwordbox\"":"easyui-textbox\"");
                dataoptions+=',width:"430px"';
                if(elem.maxLen!=0){
                    dataoptions=dataoptions+ ",validType:\"maxLength["+elem.maxLen+"]\"";
                }
                if(elem.maxLen>=250){
                    dataoptions=dataoptions+", multiline:true"
                }
            }
            eleHtml+=" data-options='"+dataoptions+"'"
            if (elem.valueType!=1&&!isEmptyStr(elem.fieldValue)){
                eleHtml+=' value="'+elem.fieldValue+'"'
            }
            if(elem.maxLen>250){
                eleHtml+=' style="height:60px"'
            }
            eleHtml+="></div>"
            wrapper.append(eleHtml)
            $.parser.parse('#'+selectorId);
        });
    }
}

function acctActionFmt(value,row,index){
    var link="";
    if(!row.isDeleted){
      link= '<a href="javascript:void(0)" onclick="openEditAccountDlg('+row.accountId+')" class="btn btn-green btn-xs">编辑</a>';
    }
    if(row.fieldPwd&&row.fieldPwd!=""){
      link=link+'&nbsp;<a href="javascript:void(0)" onclick="copyPwd('+row.accountId+')" class="btn btn-kermesinus btn-xs">复制密码</a>';
    }
    return link;
}
  //打开新增账号窗口
function openNewAccountDlg(projId){
    var url='/account/toaddaccount';
    if(projId&&projId>0){
        url=url+"?projId="+projId
    }
    document.getElementById("dlgframe").src=url;
    $('#accountDlg').dialog('setTitle',"新增账号");
    $('#accountDlg').dialog('open');
}
  //打开编辑账号窗口
function openEditAccountDlg(aid){
    document.getElementById("dlgframe").src='/account/toeditaccount?id='+aid;
    $('#accountDlg').dialog('setTitle',"编辑账号");
    $('#accountDlg').dialog('open');
}

function closeAccountDlg(reload){
    $('#accountDlg').dialog('close');
    if(reload){
        $('#accountGrid').datagrid('reload');
    }
}

function copyPwd(accid){
    $('#accountGrid').datagrid('selectRecord',accid);
    var selectedRow=$('#accountGrid').datagrid('getSelected');
    // if(selectedRow==null){
    //     showWarn("请选择一个账号");
    //     return;
    // }
    if(selectedRow.fieldPwd!=""){
        var pwd="";
        try {
            pwd=decrypt(selectedRow.fieldPwd)
        } catch (error) {
            console.log(error);
            toast("解密异常");
            return;
        }
        try {
            var isOk= cp(pwd);
            if(isOk){
            toast("已复制");
            setTimeout(function () {
            cp("");
            }, 30000);
        }
        } catch (error) {
            showError("复制失败");
            return;
        }
    }
}